home *** CD-ROM | disk | FTP | other *** search
/ Inter.Net 55-1 / Inter.Net 55-1.iso / CBuilder / Setup / BCB / data.z / algorith.cc < prev    next >
Encoding:
C/C++ Source or Header  |  1998-02-09  |  80.0 KB  |  2,535 lines

  1. #ifndef __ALGORITH_CC
  2. #define __ALGORITH_CC
  3. #pragma option push -b -a4 -Vx- -Ve- -w-inl -w-aus -w-sig
  4.  
  5. /***************************************************************************
  6.  *
  7.  * algorithm.cc - Non-inline definitions for the Standard Library algorithms
  8.  *
  9.  * $Id: algorithm.cc,v 1.4 1996/08/30 00:35:17 smithey Exp $
  10.  *
  11.  ***************************************************************************
  12.  *
  13.  * Copyright (c) 1994
  14.  * Hewlett-Packard Company
  15.  *
  16.  * Permission to use, copy, modify, distribute and sell this software
  17.  * and its documentation for any purpose is hereby granted without fee,
  18.  * provided that the above copyright notice appear in all copies and
  19.  * that both that copyright notice and this permission notice appear
  20.  * in supporting documentation.  Hewlett-Packard Company makes no
  21.  * representations about the suitability of this software for any
  22.  * purpose.  It is provided "as is" without express or implied warranty.
  23.  *
  24.  *
  25.  ***************************************************************************
  26.  *
  27.  * (c) Copyright 1994, 1995 Rogue Wave Software, Inc.
  28.  * ALL RIGHTS RESERVED *
  29.  * The software and information contained herein are proprietary to, and
  30.  * comprise valuable trade secrets of, Rogue Wave Software, Inc., which
  31.  * intends to preserve as trade secrets such software and information.
  32.  * This software is furnished pursuant to a written license agreement and
  33.  * may be used, copied, transmitted, and stored only in accordance with
  34.  * the terms of such license and with the inclusion of the above copyright
  35.  * notice.  This software and information or any other copies thereof may
  36.  * not be provided or otherwise made available to any other person.
  37.  *
  38.  * Notwithstanding any other lease or license that may pertain to, or
  39.  * accompany the delivery of, this computer software and information, the
  40.  * rights of the Government regarding its use, reproduction and disclosure
  41.  * are as set forth in Section 52.227-19 of the FARS Computer
  42.  * Software-Restricted Rights clause.
  43.  * 
  44.  * Use, duplication, or disclosure by the Government is subject to
  45.  * restrictions as set forth in subparagraph (c)(1)(ii) of the Rights in
  46.  * Technical Data and Computer Software clause at DFARS 252.227-7013.
  47.  * Contractor/Manufacturer is Rogue Wave Software, Inc.,
  48.  * P.O. Box 2328, Corvallis, Oregon 97339.
  49.  *
  50.  * This computer software and information is distributed with "restricted
  51.  * rights."  Use, duplication or disclosure is subject to restrictions as
  52.  * set forth in NASA FAR SUP 18-52.227-79 (April 1985) "Commercial
  53.  * Computer Software-Restricted Rights (April 1985)."  If the Clause at
  54.  * 18-52.227-74 "Rights in Data General" is specified in the contract,
  55.  * then the "Alternate III" clause applies.
  56.  *
  57.  **************************************************************************/
  58.  
  59. #include <stdcomp.h>
  60.  
  61. #ifndef _RWSTD_NO_NAMESPACE
  62. namespace std {
  63. #endif
  64.  
  65. //
  66. // Forward declare raw_storage_iterator 
  67. //   
  68. template <class OutputIterator, class T>
  69. class raw_storage_iterator;
  70.  
  71. //
  72. // Non-modifying sequence operations.
  73. //
  74.  
  75. template <class InputIterator, class Function>
  76. Function for_each (InputIterator first, InputIterator last, Function f)
  77. {
  78.     while (first != last) f(*first++);
  79.     return f;
  80. }
  81.  
  82. template <class InputIterator, class T>
  83. InputIterator find (InputIterator first, InputIterator last, const T& value)
  84. {
  85.     while (first != last && *first != value)
  86.         ++first;
  87.     return first;
  88. }
  89.  
  90. template <class InputIterator, class Predicate>
  91. InputIterator find_if (InputIterator first, InputIterator last, Predicate pred)
  92. {
  93.     while (first != last && !pred(*first)) ++first;
  94.     return first;
  95. }
  96.  
  97.  
  98. template <class ForwardIterator1, class ForwardIterator2, 
  99.           class Distance>
  100. ForwardIterator1 __find_end (ForwardIterator1 first1,
  101.                                   ForwardIterator1 last1,
  102.                                   ForwardIterator2 first2,
  103.                                   ForwardIterator2 last2,
  104.                                   Distance*)
  105. {
  106.    Distance d, d2;
  107.    __initialize(d,Distance(0));
  108.    __initialize(d2,Distance(0));
  109.    distance(first2,last2,d);
  110.    if (!d)
  111.      return first1;
  112.    distance(first1,last1,d2);
  113.    ForwardIterator1 save = last1;
  114.  
  115.    while (d2 >= d)
  116.    {
  117.      if (equal(first2,last2,first1))
  118.        save  = first1;
  119.      __initialize(d2,Distance(0));
  120.      distance(++first1,last1,d2);
  121.    }
  122.    return save;
  123. }
  124.  
  125.  
  126. template <class ForwardIterator1, class ForwardIterator2>
  127. ForwardIterator1 find_end (ForwardIterator1 first1,
  128.                                   ForwardIterator1 last1,
  129.                                   ForwardIterator2 first2,
  130.                                   ForwardIterator2 last2)
  131. {
  132.    return __find_end(first1,last1,first2,last2,
  133.                      distance_type(first1));
  134. }
  135.  
  136. template <class ForwardIterator1, class ForwardIterator2, 
  137.           class BinaryPredicate, class Distance>
  138. ForwardIterator1 __find_end (ForwardIterator1 first1,
  139.                                   ForwardIterator1 last1,
  140.                                   ForwardIterator2 first2,
  141.                                   ForwardIterator2 last2,
  142.                                   BinaryPredicate pred,
  143.                                   Distance*)
  144. {
  145.    Distance d, d2;
  146.    __initialize(d,Distance(0));
  147.    __initialize(d2,Distance(0));
  148.    distance(first2,last2,d);
  149.    if (!d)
  150.      return first1;
  151.    distance(first1,last1,d2);
  152.    ForwardIterator1 save = last1;
  153.  
  154.    while (d2 >= d)
  155.    {
  156.      if (equal(first2,last2,first1,pred))
  157.        save  = first1;
  158.      __initialize(d2,Distance(0));
  159.      distance(++first1,last1,d2);
  160.    }
  161.    return save;
  162. }
  163.  
  164. template <class ForwardIterator1, class ForwardIterator2,
  165.           class BinaryPredicate>
  166. ForwardIterator1 find_end (ForwardIterator1 first1,
  167.                                   ForwardIterator1 last1,
  168.                                   ForwardIterator2 first2,
  169.                                   ForwardIterator2 last2,
  170.                                   BinaryPredicate pred)
  171. {
  172.    return __find_end(first1,last1,first2,last2,
  173.                      pred,distance_type(first1));
  174. }
  175.  
  176.  
  177. template <class ForwardIterator1, class ForwardIterator2>
  178. ForwardIterator1 find_first_of (ForwardIterator1 first1, ForwardIterator1 last1,
  179.                                 ForwardIterator2 first2, ForwardIterator2 last2)
  180. {
  181.     if (first2 == last2)
  182.         return first1;
  183.     ForwardIterator1 next = first1;
  184.     while (next != last1)
  185.     {
  186.         if (find(first2,last2,*next) != last2)
  187.             return next;
  188.         next++;
  189.     }
  190.     return last1;
  191. }
  192.  
  193. template <class ForwardIterator1, class ForwardIterator2,
  194.           class BinaryPredicate>
  195. ForwardIterator1 find_first_of (ForwardIterator1 first1,ForwardIterator1 last1,
  196.                                 ForwardIterator2 first2,ForwardIterator2 last2,
  197.                                 BinaryPredicate pred)
  198. {
  199.     if (first2 == last2)
  200.         return first1;
  201.     ForwardIterator1 next = first1;
  202.     while (next != last1)
  203.     {
  204.         if (find_if(first2,last2,bind2nd(pred,*next)) != last2)
  205.             return next;
  206.         next++;
  207.     }
  208.     return last1;
  209. }
  210.  
  211.  
  212. template <class ForwardIterator>
  213. ForwardIterator adjacent_find (ForwardIterator first, ForwardIterator last)
  214. {
  215.     if (first == last) return last;
  216.     ForwardIterator next = first;
  217.     while (++next != last)
  218.     {
  219.         if (*first == *next) return first;
  220.         first = next;
  221.     }
  222.     return last;
  223. }
  224.  
  225. template <class ForwardIterator, class BinaryPredicate>
  226. ForwardIterator adjacent_find (ForwardIterator first, ForwardIterator last,
  227.                              BinaryPredicate binary_pred)
  228. {
  229.     if (first == last) return last;
  230.     ForwardIterator next = first;
  231.     while (++next != last)
  232.     {
  233.         if (binary_pred(*first, *next)) return first;
  234.         first = next;
  235.     }
  236.     return last;
  237. }
  238.  
  239. #ifndef _RWSTD_NO_CLASS_PARTIAL_SPEC
  240. template <class InputIterator, class T>
  241. iterator_traits<InputIterator>::distance_type
  242. count (InputIterator first, InputIterator last, const T& value)
  243. {
  244.     iterator_traits<InputIterator>::distance_type n = 0;
  245.     while (first != last) 
  246.         if (*first++ == value) ++n;
  247. }
  248.  
  249. template <class InputIterator, class Predicate>
  250. iterator_traits<InputIterator>::distance_type
  251. count_if (InputIterator first, InputIterator last, Predicate pred)
  252. {
  253.     iterator_traits<InputIterator>::distance_type n = 0;
  254.     while (first != last)
  255.     if (pred(*first++)) ++n;
  256. }
  257. #endif /* _RWSTD_NO_CLASS_PARTIAL_SPEC */
  258.  
  259. #ifndef _RWSTD_NO_OLD_COUNT
  260. template <class InputIterator, class T, class Size>
  261. void count (InputIterator first, InputIterator last, const T& value, Size& n)
  262. {
  263.     while (first != last) 
  264.         if (*first++ == value) ++n;
  265. }
  266.  
  267. template <class InputIterator, class Predicate, class Size>
  268. void count_if (InputIterator first, InputIterator last, Predicate pred, 
  269.                Size& n)
  270. {
  271.     while (first != last)
  272.     if (pred(*first++)) ++n;
  273. }
  274. #endif /* _RWSTD_NO_OLD_COUNT */
  275.  
  276. template <class InputIterator1, class InputIterator2>
  277. pair<InputIterator1, InputIterator2> mismatch(InputIterator1 first1,
  278.                                               InputIterator1 last1,
  279.                                               InputIterator2 first2)
  280. {
  281.     while (first1 != last1 && *first1 == *first2)
  282.     {
  283.         ++first1;
  284.         ++first2;
  285.     }
  286.     pair<InputIterator1, InputIterator2> tmp(first1, first2);
  287.     return tmp;
  288. }
  289.  
  290. template <class InputIterator1, class InputIterator2, class BinaryPredicate>
  291. pair<InputIterator1, InputIterator2> mismatch (InputIterator1 first1,
  292.                                                InputIterator1 last1,
  293.                                                InputIterator2 first2,
  294.                                                BinaryPredicate binary_pred)
  295. {
  296.     while (first1 != last1 && binary_pred(*first1, *first2))
  297.     {
  298.         ++first1;
  299.         ++first2;
  300.     }
  301.     pair<InputIterator1, InputIterator2> tmp(first1, first2);
  302.     return tmp;
  303. }
  304.  
  305. template <class ForwardIterator1, class ForwardIterator2,
  306.       class Distance1, class Distance2>
  307. ForwardIterator1 __search (ForwardIterator1 first1, ForwardIterator1 last1,
  308.                            ForwardIterator2 first2, ForwardIterator2 last2,
  309.                            Distance1*, Distance2*)
  310. {
  311.     Distance1 d1;
  312.     __initialize(d1, Distance1(0));
  313.     distance(first1, last1, d1);
  314.     Distance2 d2;
  315.     __initialize(d2, Distance2(0));
  316.     distance(first2, last2, d2);
  317.  
  318.     if (d1 < d2) return last1;
  319.  
  320.     ForwardIterator1 current1 = first1;
  321.     ForwardIterator2 current2 = first2;
  322.  
  323.     while (current2 != last2)
  324.     {
  325.         if (*current1++ != *current2++)
  326.             if (d1-- == d2)
  327.                 return last1;
  328.             else
  329.             {
  330.                 current1 = ++first1;
  331.                 current2 = first2;
  332.             }
  333.     }
  334.     return (current2 == last2) ? first1 : last1;
  335. }
  336.  
  337. template <class ForwardIterator1, class ForwardIterator2,
  338.           class BinaryPredicate, class Distance1, class Distance2>
  339. ForwardIterator1 __search (ForwardIterator1 first1, ForwardIterator1 last1,
  340.                            ForwardIterator2 first2, ForwardIterator2 last2,
  341.                            BinaryPredicate binary_pred, Distance1*, Distance2*)
  342. {
  343.     Distance1 d1;
  344.     __initialize(d1, Distance1(0));
  345.     distance(first1, last1, d1);
  346.     Distance2 d2;
  347.     __initialize(d2, Distance2(0));
  348.     distance(first2, last2, d2);
  349.  
  350.     if (d1 < d2) return last1;
  351.  
  352.     ForwardIterator1 current1 = first1;
  353.     ForwardIterator2 current2 = first2;
  354.  
  355.     while (current2 != last2)
  356.     {
  357.         if (!binary_pred(*current1++, *current2++))
  358.             if (d1-- == d2)
  359.                 return last1;
  360.             else
  361.             {
  362.                 current1 = ++first1;
  363.                 current2 = first2;
  364.             }
  365.     }
  366.     return (current2 == last2) ? first1 : last1;
  367. }
  368.  
  369. template <class ForwardIterator, class Distance, class Size, class T>
  370. ForwardIterator __search_n (ForwardIterator first, ForwardIterator last,
  371.                             Distance*, Size count, const T& value)
  372. {
  373.     Distance d;
  374.     __initialize(d, Distance(0));
  375.     distance(first, last, d);
  376.  
  377.     if (d < count || count <= 0) return last;
  378.  
  379.     Distance        span    = d - count;
  380.     Size            matches = 0;
  381.     ForwardIterator current = first;
  382.  
  383.     while (current != last)
  384.     {
  385.         if (*current++ != value)
  386.         {
  387.             if (span < matches + 1)
  388.               return last;
  389.             span   -= matches + 1;
  390.             matches = 0;
  391.             first   = current;
  392.         }
  393.         else
  394.             if (++matches == count)
  395.                 return first;
  396.     }
  397.  
  398.     return last;
  399. }
  400.  
  401. template <class ForwardIterator, class Distance, class Size, class T,
  402.           class BinaryPredicate>
  403. ForwardIterator __search_n (ForwardIterator first, ForwardIterator last,
  404.                             Distance*, Size count, const T& value,
  405.                             BinaryPredicate pred)
  406. {
  407.     Distance d;
  408.     __initialize(d, Distance(0));
  409.     distance(first, last, d);
  410.  
  411.     if (d < count || count <= 0) return last;
  412.  
  413.     Distance        span    = d - count;
  414.     Size            matches = 0;
  415.     ForwardIterator current = first;
  416.  
  417.     while (current != last)
  418.     {
  419.         if (!pred(*current++, value))
  420.         {
  421.             if (span < matches + 1)
  422.               return last;
  423.             span   -= matches + 1;
  424.             matches = 0;
  425.             first   = current;
  426.         }
  427.         else
  428.             if (++matches == count)
  429.                 return first;
  430.     }
  431.  
  432.     return last;
  433. }
  434.  
  435. //
  436. // Modifying sequence operations.
  437. //
  438.  
  439. template <class InputIterator, class OutputIterator>
  440. OutputIterator copy (InputIterator first, InputIterator last,
  441.                      OutputIterator result)
  442. {
  443.     while (first != last) *result++ = *first++;
  444.     return result;
  445. }
  446.  
  447. template <class BidirectionalIterator1, class BidirectionalIterator2>
  448. BidirectionalIterator2 copy_backward (BidirectionalIterator1 first, 
  449.                                       BidirectionalIterator1 last, 
  450.                                       BidirectionalIterator2 result)
  451. {
  452.     while (first != last) *--result = *--last;
  453.     return result;
  454. }
  455.  
  456. template <class ForwardIterator1, class ForwardIterator2>
  457. ForwardIterator2 swap_ranges (ForwardIterator1 first1, ForwardIterator1 last1,
  458.                               ForwardIterator2 first2)
  459. {
  460.     while (first1 != last1) iter_swap(first1++, first2++);
  461.     return first2;
  462. }
  463.  
  464. template <class InputIterator, class OutputIterator, class UnaryOperation>
  465. OutputIterator transform (InputIterator first, InputIterator last,
  466.                           OutputIterator result, UnaryOperation op)
  467. {
  468.     while (first != last) *result++ = op(*first++);
  469.     return result;
  470. }
  471.  
  472. template <class InputIterator1, class InputIterator2, class OutputIterator,
  473.       class BinaryOperation>
  474. OutputIterator transform (InputIterator1 first1, InputIterator1 last1,
  475.                           InputIterator2 first2, OutputIterator result,
  476.                           BinaryOperation binary_op)
  477. {
  478.     while (first1 != last1) *result++ = binary_op(*first1++, *first2++);
  479.     return result;
  480. }
  481.  
  482. template <class ForwardIterator, class T>
  483. void replace (ForwardIterator first, ForwardIterator last, const T& old_value,
  484.               const T& new_value)
  485. {
  486.     while (first != last)
  487.     {
  488.         if (*first == old_value) *first = new_value;
  489.         ++first;
  490.     }
  491. }
  492.  
  493. template <class ForwardIterator, class Predicate, class T>
  494. void replace_if (ForwardIterator first, ForwardIterator last, Predicate pred,
  495.                  const T& new_value)
  496. {
  497.     while (first != last)
  498.     {
  499.         if (pred(*first)) *first = new_value;
  500.         ++first;
  501.     }
  502. }
  503.  
  504. template <class InputIterator, class OutputIterator, class T>
  505. OutputIterator replace_copy (InputIterator first, InputIterator last,
  506.                              OutputIterator result, const T& old_value,
  507.                              const T& new_value)
  508. {
  509.     while (first != last)
  510.     {
  511.         *result++ = *first == old_value ? new_value : *first;
  512.         ++first;
  513.     }
  514.     return result;
  515. }
  516.  
  517. template <class Iterator, class OutputIterator, class Predicate, class T>
  518. OutputIterator replace_copy_if (Iterator first, Iterator last,
  519.                                 OutputIterator result, Predicate pred,
  520.                                 const T& new_value)
  521. {
  522.     while (first != last)
  523.     {
  524.         if(pred(*first))
  525.            *result++ = new_value;
  526.         else
  527.            *result++ = *first;
  528.         ++first;
  529.     }
  530.     return result;
  531. }
  532.  
  533. template <class ForwardIterator, class T>
  534. #ifdef _RWSTD_FILL_NAME_CLASH
  535. void std_fill (ForwardIterator first, ForwardIterator last, const T& value)
  536. #else
  537. void fill (ForwardIterator first, ForwardIterator last, const T& value)
  538. #endif
  539. {
  540.     while (first != last) *first++ = value;
  541. }
  542.  
  543. template <class OutputIterator, class Size, class T>
  544. void fill_n (OutputIterator first, Size n, const T& value)
  545. {
  546.     while (n-- > 0) *first++ = value;
  547. }
  548.  
  549. template <class ForwardIterator, class Generator>
  550. void generate (ForwardIterator first, ForwardIterator last, Generator gen)
  551. {
  552.     while (first != last) *first++ = gen();
  553. }
  554.  
  555. template <class OutputIterator, class Size, class Generator>
  556. void generate_n (OutputIterator first, Size n, Generator gen)
  557. {
  558.     while (n-- > 0) *first++ = gen();
  559. }
  560.  
  561. template <class InputIterator, class OutputIterator, class T>
  562. OutputIterator remove_copy (InputIterator first, InputIterator last,
  563.                             OutputIterator result, const T& value)
  564. {
  565.     while (first != last)
  566.     {
  567.         if (*first != value) *result++ = *first;
  568.         ++first;
  569.     }
  570.     return result;
  571. }
  572.  
  573. template <class InputIterator, class OutputIterator, class Predicate>
  574. OutputIterator remove_copy_if (InputIterator first, InputIterator last,
  575.                                OutputIterator result, Predicate pred)
  576. {
  577.     while (first != last)
  578.     {
  579.         if (!pred(*first)) *result++ = *first;
  580.         ++first;
  581.     }
  582.     return result;
  583. }
  584.  
  585. template <class InputIterator, class ForwardIterator>
  586. ForwardIterator __unique_copy (InputIterator first, InputIterator last,
  587.                                ForwardIterator result, forward_iterator_tag)
  588. {
  589.     *result = *first;
  590.     while (++first != last)
  591.         if (*result != *first) *++result = *first;
  592.     return ++result;
  593. }
  594.  
  595. template <class InputIterator, class OutputIterator, class T>
  596. OutputIterator __unique_copy (InputIterator first, InputIterator last,
  597.                               OutputIterator result, T*)
  598. {
  599.     T value = *first;
  600.     *result = value;
  601.     while (++first != last)
  602.     {
  603.         if (value != *first)
  604.         {
  605.             value = *first;
  606.             *++result = value;
  607.         }
  608.     }
  609.     return ++result;
  610. }
  611.  
  612. template <class InputIterator, class ForwardIterator, class BinaryPredicate>
  613. ForwardIterator __unique_copy (InputIterator first, InputIterator last,
  614.                                ForwardIterator result, 
  615.                                BinaryPredicate binary_pred,
  616.                                forward_iterator_tag)
  617. {
  618.     *result = *first;
  619.     while (++first != last)
  620.         if (!binary_pred(*result, *first)) *++result = *first;
  621.     return ++result;
  622. }
  623.  
  624. template <class InputIterator, class OutputIterator, class BinaryPredicate,
  625.           class T>
  626. OutputIterator __unique_copy (InputIterator first, InputIterator last,
  627.                               OutputIterator result,
  628.                               BinaryPredicate binary_pred, T*)
  629. {
  630.     T value = *first;
  631.     *result = value;
  632.     while (++first != last)
  633.     {
  634.         if (!binary_pred(value, *first))
  635.         {
  636.             value = *first;
  637.             *++result = value;
  638.         }
  639.     }
  640.     return ++result;
  641. }
  642.  
  643. template <class BidirectionalIterator>
  644. void __reverse (BidirectionalIterator first, BidirectionalIterator last,
  645.                 bidirectional_iterator_tag)
  646. {
  647.     while (true)
  648.         if (first == last || first == --last)
  649.             return;
  650.         else
  651.             iter_swap(first++, last);
  652. }
  653.  
  654. template <class RandomAccessIterator>
  655. void __reverse (RandomAccessIterator first, RandomAccessIterator last,
  656.                 random_access_iterator_tag)
  657. {
  658.     while (first < last) iter_swap(first++, --last);
  659. }
  660.  
  661. template <class BidirectionalIterator, class OutputIterator>
  662. OutputIterator reverse_copy (BidirectionalIterator first,
  663.                              BidirectionalIterator last,
  664.                              OutputIterator result)
  665. {
  666.     while (first != last) *result++ = *--last;
  667.     return result;
  668. }
  669.  
  670. template <class ForwardIterator, class Distance>
  671. void __rotate (ForwardIterator first, ForwardIterator middle,
  672.                ForwardIterator last, Distance*, forward_iterator_tag)
  673. {
  674.     for (ForwardIterator i = middle; ;)
  675.     {
  676.         iter_swap(first++, i++);
  677.         if (first == middle)
  678.         {
  679.             if (i == last) return;
  680.                 middle = i;
  681.         }
  682.         else if (i == last)
  683.             i = middle;
  684.     }
  685. }
  686.  
  687. template <class EuclideanRingElement>
  688. EuclideanRingElement __gcd (EuclideanRingElement m, EuclideanRingElement n)
  689. {
  690.     while (n != 0)
  691.     {
  692.         EuclideanRingElement t = m % n;
  693.         m = n;
  694.         n = t;
  695.     }
  696.     return m;
  697. }
  698.  
  699. template <class RandomAccessIterator, class Distance, class T>
  700. void __rotate_cycle (RandomAccessIterator first, RandomAccessIterator last,
  701.                      RandomAccessIterator initial, Distance shift, T*)
  702. {
  703.     T value = *initial;
  704.     RandomAccessIterator ptr1 = initial;
  705.     RandomAccessIterator ptr2 = ptr1 + shift;
  706.     while (ptr2 != initial)
  707.     {
  708.         *ptr1 = *ptr2;
  709.         ptr1 = ptr2;
  710.         if (last - ptr2 > shift)
  711.             ptr2 += shift;
  712.         else
  713.             ptr2 = first + (shift - (last - ptr2));
  714.     }
  715.     *ptr1 = value;
  716. }
  717.  
  718. template <class RandomAccessIterator, class Distance>
  719. void __rotate (RandomAccessIterator first, RandomAccessIterator middle,
  720.                RandomAccessIterator last, Distance*,
  721.                random_access_iterator_tag)
  722. {
  723.     Distance n = __gcd(last - first, middle - first);
  724.     while (n--)
  725.         __rotate_cycle(first, last, first + n, middle - first,
  726.                        _RWSTD_VALUE_TYPE(first));
  727. }
  728.  
  729.  
  730. #ifndef _RWSTD_NO_NAMESPACE
  731. }
  732. namespace __rwstd {
  733. #endif
  734. extern unsigned _RWSTDExport long __long_random (unsigned long);
  735. #ifndef _RWSTD_NO_NAMESPACE
  736. }
  737. namespace std {
  738. #endif
  739.  
  740. template <class RandomAccessIterator, class Distance>
  741. void __random_shuffle (RandomAccessIterator first, RandomAccessIterator last,
  742.                        Distance*)
  743. {
  744.     if (!(first == last))
  745.         for (RandomAccessIterator i = first + 1; i != last; ++i)
  746.             iter_swap(i, first + Distance(__RWSTD::__long_random((i - first) + 1)));
  747. }
  748.  
  749. template <class RandomAccessIterator, class RandomNumberGenerator>
  750. void random_shuffle (RandomAccessIterator first, RandomAccessIterator last,
  751.                      RandomNumberGenerator& rand)
  752. {
  753.     if (!(first == last))
  754.         for (RandomAccessIterator i = first + 1; i != last; ++i)
  755.             iter_swap(i, first + rand((i - first) + 1));
  756. }
  757.  
  758. template <class BidirectionalIterator, class Predicate>
  759. BidirectionalIterator partition (BidirectionalIterator first,
  760.                                  BidirectionalIterator last, Predicate pred)
  761. {
  762.     while (true)
  763.     {
  764.         while (true)
  765.         {
  766.             if (first == last)
  767.                 return first;
  768.             else if (pred(*first))
  769.                 ++first;
  770.             else
  771.                 break;
  772.         }
  773.         --last;
  774.         while (true)
  775.         {
  776.             if (first == last)
  777.                 return first;
  778.             else if (!pred(*last))
  779.                 --last;
  780.             else
  781.                 break;
  782.         }
  783.         iter_swap(first, last);
  784.         ++first;
  785.     }
  786. }
  787.  
  788. template <class BidirectionalIterator, class Predicate, class Distance>
  789. BidirectionalIterator __inplace_stable_partition (BidirectionalIterator first,
  790.                                                   BidirectionalIterator last,
  791.                                                   Predicate pred,
  792.                                                   Distance len)
  793. {
  794.     if (len == 1) return pred(*first) ? last : first;
  795.     BidirectionalIterator middle = first;
  796.     advance(middle, len / 2);
  797.     BidirectionalIterator 
  798.     first_cut = __inplace_stable_partition(first, middle, pred, len / 2);
  799.     BidirectionalIterator 
  800.     second_cut = __inplace_stable_partition(middle, last, pred, len - len / 2);
  801.     rotate(first_cut, middle, second_cut);
  802.     __initialize(len, Distance(0));
  803.     distance(middle, second_cut, len);
  804.     advance(first_cut, len);
  805.     return first_cut;
  806. }
  807.  
  808. template <class BidirectionalIterator, class Pointer, class Predicate,
  809.           class Distance, class T>
  810. BidirectionalIterator __stable_partition_adaptive (BidirectionalIterator first,
  811.                                                    BidirectionalIterator last,
  812.                                                    Predicate pred, Distance len,
  813.                                                    Pointer buffer,
  814.                                                    Distance buffer_size,
  815.                                                    Distance& fill_pointer, T*)
  816. {
  817.     if (len <= buffer_size)
  818.     {
  819.         len = 0;
  820.         BidirectionalIterator result1 = first;
  821.         Pointer result2 = buffer;
  822.         while (first != last && len < fill_pointer)
  823.         {
  824.             if (pred(*first))
  825.                 *result1++ = *first++;
  826.             else
  827.             {
  828.                 *result2++ = *first++;
  829.                 ++len;
  830.             }
  831.         }
  832.         if (first != last)
  833.         {
  834.             raw_storage_iterator<Pointer, T> result3(result2);
  835.             while (first != last)
  836.             {
  837.                 if (pred(*first))
  838.                     *result1++ = *first++;
  839.                 else
  840.                 {
  841.                     *result3++ = *first++;
  842.                     ++len;
  843.                 }
  844.             }
  845.             fill_pointer = len;
  846.         }
  847.         copy(buffer, buffer + len, result1);
  848.         return result1;
  849.     }
  850.     BidirectionalIterator middle = first;
  851.     advance(middle, len / 2);
  852.     BidirectionalIterator first_cut = __stable_partition_adaptive
  853.      (first, middle, pred, len / 2, buffer, buffer_size, fill_pointer, (T*)0);
  854.     BidirectionalIterator second_cut = __stable_partition_adaptive
  855.      (middle, last, pred, len-len/2, buffer, buffer_size, fill_pointer, (T*)0);
  856.     rotate(first_cut, middle, second_cut);
  857.     __initialize(len, Distance(0));
  858.     distance(middle, second_cut, len);
  859.     advance(first_cut, len);
  860.     return first_cut;
  861. }
  862.  
  863. template <class BidirectionalIterator, class Predicate, class Pointer,
  864.           class Distance>
  865. BidirectionalIterator __stable_partition (BidirectionalIterator first,
  866.                                           BidirectionalIterator last,
  867.                                           Predicate pred, Distance len,
  868.                                           pair<Pointer, Distance> p)
  869. {
  870.     if (p.first == 0)
  871.         return __inplace_stable_partition(first, last, pred, len);
  872.     Distance fill_pointer = 0;
  873.     BidirectionalIterator result = 
  874.         __stable_partition_adaptive(first, last, pred, len, p.first,
  875.                                     p.second, fill_pointer,
  876.                                     _RWSTD_VALUE_TYPE(first)); 
  877.     destroy(p.first, p.first + fill_pointer);
  878.     return_temporary_buffer(p.first);
  879.     return result;
  880. }
  881.  
  882. //
  883. // Sorting and related operations.
  884. //
  885.  
  886. template <class RandomAccessIterator, class T>
  887. RandomAccessIterator __unguarded_partition (RandomAccessIterator first, 
  888.                                             RandomAccessIterator last, 
  889.                                             T pivot)
  890. {
  891.     while (true)
  892.     {
  893.         while (*first < pivot) ++first;
  894.         --last;
  895.         while (pivot < *last) --last;
  896.         if (!(first < last)) return first;
  897.         iter_swap(first, last);
  898.         ++first;
  899.     }
  900. }    
  901.  
  902. template <class RandomAccessIterator, class T, class Compare>
  903. RandomAccessIterator __unguarded_partition (RandomAccessIterator first, 
  904.                                             RandomAccessIterator last, 
  905.                                             T pivot, Compare _RWSTD_COMP)
  906. {
  907.     while (true)
  908.     {
  909.         while (_RWSTD_COMP(*first, pivot)) ++first;
  910.         --last;
  911.         while (_RWSTD_COMP(pivot, *last)) --last;
  912.         if (!(first < last)) return first;
  913.         iter_swap(first, last);
  914.         ++first;
  915.     }
  916. }
  917.  
  918. const int __stl_threshold = 16;
  919.  
  920. template <class RandomAccessIterator, class T>
  921. void __quick_sort_loop_aux (RandomAccessIterator first,
  922.                             RandomAccessIterator last, T*)
  923. {
  924.     while (last - first > __stl_threshold)
  925.     {
  926.         RandomAccessIterator cut = __unguarded_partition
  927.             (first, last, T(__median(*first, *(first + (last - first)/2),
  928.                          *(last - 1))));
  929.         if (cut - first >= last - cut)
  930.         {
  931.             __quick_sort_loop(cut, last);
  932.             last = cut;
  933.         }
  934.         else
  935.         {
  936.             __quick_sort_loop(first, cut);
  937.             first = cut;
  938.         }
  939.     }
  940. }
  941.  
  942. template <class RandomAccessIterator, class T, class Compare>
  943. void __quick_sort_loop_aux (RandomAccessIterator first, 
  944.                             RandomAccessIterator last, T*, Compare _RWSTD_COMP)
  945. {
  946.     while (last - first > __stl_threshold)
  947.     {
  948.         RandomAccessIterator cut = __unguarded_partition
  949.             (first, last, T(__median(*first, *(first + (last - first)/2), 
  950.                        *(last - 1), _RWSTD_COMP)), _RWSTD_COMP);
  951.         if (cut - first >= last - cut)
  952.         {
  953.             __quick_sort_loop(cut, last, _RWSTD_COMP);
  954.             last = cut;
  955.         }
  956.         else
  957.         {
  958.             __quick_sort_loop(first, cut, _RWSTD_COMP);
  959.             first = cut;
  960.         }
  961.     }
  962. }
  963.  
  964.  
  965.  
  966. template <class RandomAccessIterator, class T>
  967. void __unguarded_linear_insert (RandomAccessIterator last, T value)
  968. {
  969.     RandomAccessIterator next = last;
  970.     --next;
  971.     while (value < *next)
  972.     {
  973.         *last = *next;
  974.         last = next--;
  975.     }
  976.     *last = value;
  977. }
  978.  
  979. template <class RandomAccessIterator, class T, class Compare>
  980. void __unguarded_linear_insert (RandomAccessIterator last,T value,Compare _RWSTD_COMP)
  981. {
  982.     RandomAccessIterator next = last;
  983.     --next;  
  984.     while (_RWSTD_COMP(value , *next))
  985.     {
  986.         *last = *next;
  987.         last = next--;
  988.     }
  989.     *last = value;
  990. }
  991.  
  992. template <class RandomAccessIterator>
  993. void __insertion_sort (RandomAccessIterator first, RandomAccessIterator last)
  994. {
  995.     if (!(first == last))
  996.         for (RandomAccessIterator i = first + 1; i != last; ++i)
  997.             __linear_insert(first, i, _RWSTD_VALUE_TYPE(first));
  998. }
  999.  
  1000. template <class RandomAccessIterator, class Compare>
  1001. void __insertion_sort (RandomAccessIterator first,
  1002.                        RandomAccessIterator last, Compare _RWSTD_COMP)
  1003. {
  1004.     if (!(first == last))
  1005.         for (RandomAccessIterator i = first + 1; i != last; ++i)
  1006.             __linear_insert(first, i, _RWSTD_VALUE_TYPE(first), _RWSTD_COMP);
  1007. }
  1008.  
  1009. template <class RandomAccessIterator, class T>
  1010. void __unguarded_insertion_sort_aux (RandomAccessIterator first, 
  1011.                                      RandomAccessIterator last, T*)
  1012. {
  1013.     for (RandomAccessIterator i = first; i != last; ++i)
  1014.         __unguarded_linear_insert(i, T(*i));
  1015. }
  1016.  
  1017. template <class RandomAccessIterator, class T, class Compare>
  1018. void __unguarded_insertion_sort_aux (RandomAccessIterator first, 
  1019.                                      RandomAccessIterator last,
  1020.                                      T*, Compare _RWSTD_COMP)
  1021. {
  1022.     for (RandomAccessIterator i = first; i != last; ++i)
  1023.         __unguarded_linear_insert(i, T(*i), _RWSTD_COMP);
  1024. }
  1025.  
  1026.  
  1027. template <class RandomAccessIterator>
  1028. void __final_insertion_sort (RandomAccessIterator first, 
  1029.                                     RandomAccessIterator last)
  1030. {
  1031.     if (last - first > __stl_threshold)
  1032.     {
  1033.         __insertion_sort(first, first + __stl_threshold);
  1034.         __unguarded_insertion_sort(first + __stl_threshold, last);
  1035.     }
  1036.     else
  1037.         __insertion_sort(first, last);
  1038. }
  1039.  
  1040. template <class RandomAccessIterator, class Compare>
  1041. void __final_insertion_sort (RandomAccessIterator first, 
  1042.                              RandomAccessIterator last, Compare _RWSTD_COMP)
  1043. {
  1044.     if (last - first > __stl_threshold)
  1045.     {
  1046.         __insertion_sort(first, first + __stl_threshold, _RWSTD_COMP);
  1047.         __unguarded_insertion_sort(first + __stl_threshold, last, _RWSTD_COMP);
  1048.     }
  1049.     else
  1050.         __insertion_sort(first, last, _RWSTD_COMP);
  1051. }
  1052.  
  1053. template <class RandomAccessIterator1, class RandomAccessIterator2,
  1054.           class Distance>
  1055. void __merge_sort_loop (RandomAccessIterator1 first,
  1056.                         RandomAccessIterator1 last, 
  1057.                         RandomAccessIterator2 result, Distance step_size)
  1058. {
  1059.     Distance two_step = 2 * step_size;
  1060.  
  1061.     while (last - first >= two_step)
  1062.     {
  1063.         result = merge(first, first + step_size,
  1064.                        first + step_size, first + two_step, result);
  1065.         first += two_step;
  1066.     }
  1067.     step_size = min(Distance(last - first), step_size);
  1068.  
  1069.     merge(first, first + step_size, first + step_size, last, result);
  1070. }
  1071.  
  1072. template <class RandomAccessIterator1, class RandomAccessIterator2,
  1073.           class Distance, class Compare>
  1074. void __merge_sort_loop (RandomAccessIterator1 first,
  1075.                         RandomAccessIterator1 last, 
  1076.                         RandomAccessIterator2 result, Distance step_size,
  1077.                         Compare _RWSTD_COMP)
  1078. {
  1079.     Distance two_step = 2 * step_size;
  1080.  
  1081.     while (last - first >= two_step)
  1082.     {
  1083.         result = merge(first, first + step_size,
  1084.                        first + step_size, first + two_step, result, _RWSTD_COMP);
  1085.         first += two_step;
  1086.     }
  1087.     step_size = min(Distance(last - first), step_size);
  1088.  
  1089.     merge(first, first + step_size, first + step_size, last, result, _RWSTD_COMP);
  1090. }
  1091.  
  1092. const int __stl_chunk_size = 7;
  1093.     
  1094. template <class RandomAccessIterator, class Distance>
  1095. void __chunk_insertion_sort (RandomAccessIterator first, 
  1096.                              RandomAccessIterator last, Distance chunk_size)
  1097. {
  1098.     while (last - first >= chunk_size)
  1099.     {
  1100.         __insertion_sort(first, first + chunk_size);
  1101.         first += chunk_size;
  1102.     }
  1103.     __insertion_sort(first, last);
  1104. }
  1105.  
  1106. template <class RandomAccessIterator, class Distance, class Compare>
  1107. void __chunk_insertion_sort (RandomAccessIterator first, 
  1108.                              RandomAccessIterator last,
  1109.                              Distance chunk_size, Compare _RWSTD_COMP)
  1110. {
  1111.     while (last - first >= chunk_size)
  1112.     {
  1113.         __insertion_sort(first, first + chunk_size, _RWSTD_COMP);
  1114.         first += chunk_size;
  1115.     }
  1116.     __insertion_sort(first, last, _RWSTD_COMP);
  1117. }
  1118.  
  1119. template <class RandomAccessIterator, class Pointer, class Distance, class T>
  1120. void __merge_sort_with_buffer (RandomAccessIterator first, 
  1121.                                RandomAccessIterator last,
  1122.                                Pointer buffer, Distance*, T*)
  1123. {
  1124.     Distance len = last - first;
  1125.     Pointer buffer_last = buffer + len;
  1126.  
  1127.     Distance step_size = __stl_chunk_size;
  1128.     __chunk_insertion_sort(first, last, step_size);
  1129.  
  1130.     while (step_size < len)
  1131.     {
  1132.         __merge_sort_loop(first, last, buffer, step_size);
  1133.         step_size *= 2;
  1134.         __merge_sort_loop(buffer, buffer_last, first, step_size);
  1135.         step_size *= 2;
  1136.     }
  1137. }
  1138.  
  1139. template <class RandomAccessIterator, class Pointer, class Distance, class T,
  1140.           class Compare>
  1141. void __merge_sort_with_buffer (RandomAccessIterator first, 
  1142.                                RandomAccessIterator last, Pointer buffer,
  1143.                                Distance*, T*, Compare _RWSTD_COMP)
  1144. {
  1145.     Distance len = last - first;
  1146.     Pointer buffer_last = buffer + len;
  1147.  
  1148.     Distance step_size = __stl_chunk_size;
  1149.     __chunk_insertion_sort(first, last, step_size, _RWSTD_COMP);
  1150.  
  1151.     while (step_size < len)
  1152.     {
  1153.         __merge_sort_loop(first, last, buffer, step_size, _RWSTD_COMP);
  1154.         step_size *= 2;
  1155.         __merge_sort_loop(buffer, buffer_last, first, step_size, _RWSTD_COMP);
  1156.         step_size *= 2;
  1157.     }
  1158. }
  1159.  
  1160. template <class RandomAccessIterator, class Pointer, class Distance, class T>
  1161. void __stable_sort_adaptive (RandomAccessIterator first, 
  1162.                              RandomAccessIterator last, Pointer buffer,
  1163.                              Distance buffer_size, T*)
  1164. {
  1165.     Distance len = (last - first + 1) / 2;
  1166.     RandomAccessIterator middle = first + len;
  1167.     if (len > buffer_size)
  1168.     {
  1169.       __stable_sort_adaptive(first, middle, buffer, buffer_size, 
  1170.                  _RWSTD_STATIC_CAST(T*,0));
  1171.       __stable_sort_adaptive(middle, last, buffer, buffer_size, 
  1172.                  _RWSTD_STATIC_CAST(T*,0));
  1173.     }
  1174.     else
  1175.       {
  1176.         __merge_sort_with_buffer(first, middle, buffer, 
  1177.                  _RWSTD_STATIC_CAST(Distance*,0),
  1178.                  _RWSTD_STATIC_CAST(T*,0));
  1179.         __merge_sort_with_buffer(middle, last, buffer, 
  1180.                  _RWSTD_STATIC_CAST(Distance*,0),
  1181.                  _RWSTD_STATIC_CAST(T*,0));
  1182.     }
  1183.     __merge_adaptive(first, middle, last, Distance(middle - first), 
  1184.              Distance(last - middle), buffer, buffer_size, _RWSTD_STATIC_CAST(T*,0));
  1185. }
  1186.  
  1187. template <class RandomAccessIterator, class Pointer, class Distance, class T,
  1188.           class Compare>
  1189. void __stable_sort_adaptive (RandomAccessIterator first, 
  1190.                              RandomAccessIterator last, Pointer buffer,
  1191.                              Distance buffer_size, T*, Compare _RWSTD_COMP)
  1192. {
  1193.     Distance len = (last - first + 1) / 2;
  1194.     RandomAccessIterator middle = first + len;
  1195.     if (len > buffer_size)
  1196.     {
  1197.         __stable_sort_adaptive(first, middle, buffer, buffer_size,_RWSTD_STATIC_CAST(T*,0),_RWSTD_COMP);
  1198.         __stable_sort_adaptive(middle, last, buffer, buffer_size, _RWSTD_STATIC_CAST(T*,0),_RWSTD_COMP);
  1199.     }
  1200.     else
  1201.     {
  1202.       __merge_sort_with_buffer(first, middle, buffer, _RWSTD_STATIC_CAST(Distance*,0),
  1203.                    _RWSTD_STATIC_CAST(T*,0), _RWSTD_COMP);
  1204.       __merge_sort_with_buffer(middle, last, buffer, _RWSTD_STATIC_CAST(Distance*,0),
  1205.                    _RWSTD_STATIC_CAST(T*,0), _RWSTD_COMP);
  1206.     }
  1207.     __merge_adaptive(first, middle, last, Distance(middle - first), 
  1208.                      Distance(last-middle), buffer, buffer_size, _RWSTD_STATIC_CAST(T*,0),_RWSTD_COMP);
  1209. }
  1210.  
  1211. template <class RandomAccessIterator, class T>
  1212. void __partial_sort (RandomAccessIterator first, RandomAccessIterator middle,
  1213.                      RandomAccessIterator last, T*)
  1214. {
  1215.     make_heap(first, middle);
  1216.     for (RandomAccessIterator i = middle; i < last; ++i)
  1217.         if (*i < *first) 
  1218.             __pop_heap(first, middle, i, T(*i), distance_type(first));
  1219.     sort_heap(first, middle);
  1220. }
  1221.  
  1222. template <class RandomAccessIterator, class T, class Compare>
  1223. void __partial_sort (RandomAccessIterator first, RandomAccessIterator middle,
  1224.                      RandomAccessIterator last, T*, Compare _RWSTD_COMP)
  1225. {
  1226.     make_heap(first, middle, _RWSTD_COMP);
  1227.     for (RandomAccessIterator i = middle; i < last; ++i)
  1228.         if (_RWSTD_COMP(*i,*first)) 
  1229.             __pop_heap(first, middle, i, T(*i), _RWSTD_COMP, distance_type(first));
  1230.     sort_heap(first, middle, _RWSTD_COMP);
  1231. }
  1232.  
  1233. template <class InputIterator, class RandomAccessIterator, class Distance,
  1234.           class T>
  1235. RandomAccessIterator __partial_sort_copy (InputIterator first,
  1236.                                           InputIterator last,
  1237.                                           RandomAccessIterator result_first,
  1238.                                           RandomAccessIterator result_last, 
  1239.                                           Distance*, T*)
  1240. {
  1241.     if (result_first == result_last) return result_last;
  1242.     RandomAccessIterator result_real_last = result_first;
  1243.     while(first != last && result_real_last != result_last)
  1244.         *result_real_last++ = *first++;
  1245.     make_heap(result_first, result_real_last);
  1246.     while (first != last)
  1247.     {
  1248.         if (*first < *result_first) 
  1249.             __adjust_heap(result_first, Distance(0),
  1250.                           Distance(result_real_last - result_first),
  1251.                           T(*first));
  1252.         ++first;
  1253.     }
  1254.     sort_heap(result_first, result_real_last);
  1255.     return result_real_last;
  1256. }
  1257.  
  1258. template <class InputIterator, class RandomAccessIterator, class Compare,
  1259.           class Distance, class T>
  1260. RandomAccessIterator __partial_sort_copy (InputIterator first,
  1261.                                           InputIterator last,
  1262.                                           RandomAccessIterator result_first,
  1263.                                           RandomAccessIterator result_last,
  1264.                                           Compare _RWSTD_COMP, Distance*, T*)
  1265. {
  1266.     if (result_first == result_last) return result_last;
  1267.     RandomAccessIterator result_real_last = result_first;
  1268.     while(first != last && result_real_last != result_last)
  1269.         *result_real_last++ = *first++;
  1270.     make_heap(result_first, result_real_last, _RWSTD_COMP);
  1271.     while (first != last)
  1272.     {
  1273.         if (_RWSTD_COMP(*first,*result_first)) 
  1274.             __adjust_heap(result_first, Distance(0),
  1275.                           Distance(result_real_last - result_first), T(*first),
  1276.                           _RWSTD_COMP);
  1277.         ++first;
  1278.     }
  1279.     sort_heap(result_first, result_real_last, _RWSTD_COMP);
  1280.     return result_real_last;
  1281. }
  1282.  
  1283. template <class RandomAccessIterator, class T>
  1284. void __nth_element (RandomAccessIterator first, RandomAccessIterator nth,
  1285.                     RandomAccessIterator last, T*)
  1286. {
  1287.     while (last - first > 3)
  1288.     {
  1289.         RandomAccessIterator cut = __unguarded_partition
  1290.             (first, last, T(__median(*first, *(first + (last - first)/2),
  1291.                          *(last - 1))));
  1292.         if (cut <= nth)
  1293.             first = cut;
  1294.         else 
  1295.             last = cut;
  1296.     }
  1297.     __insertion_sort(first, last);
  1298. }
  1299.  
  1300. template <class RandomAccessIterator, class T, class Compare>
  1301. void __nth_element (RandomAccessIterator first, RandomAccessIterator nth,
  1302.                     RandomAccessIterator last, T*, Compare _RWSTD_COMP)
  1303. {
  1304.     while (last - first > 3)
  1305.     {
  1306.         RandomAccessIterator cut = __unguarded_partition
  1307.             (first, last, T(__median(*first, *(first + (last - first)/2), 
  1308.                      *(last - 1), _RWSTD_COMP)), _RWSTD_COMP);
  1309.         if (cut <= nth)
  1310.             first = cut;
  1311.         else 
  1312.             last = cut;
  1313.         }
  1314.     __insertion_sort(first, last, _RWSTD_COMP);
  1315. }
  1316.  
  1317. //
  1318. // Binary search.
  1319. //
  1320.  
  1321. template <class ForwardIterator, class T, class Distance>
  1322. ForwardIterator __lower_bound (ForwardIterator first, ForwardIterator last,
  1323.                                const T& value, Distance*,
  1324.                                forward_iterator_tag)
  1325. {
  1326.     Distance len;
  1327.     __initialize(len, Distance(0));
  1328.     distance(first, last, len);
  1329.     Distance half;
  1330.     ForwardIterator middle;
  1331.  
  1332.     while (len > 0)
  1333.     {
  1334.         half = len / 2;
  1335.         middle = first;
  1336.         advance(middle, half);
  1337.         if (*middle < value)
  1338.         {
  1339.             first = middle;
  1340.             ++first;
  1341.             len = len - half - 1;
  1342.         }
  1343.         else
  1344.             len = half;
  1345.     }
  1346.     return first;
  1347. }
  1348.  
  1349. template <class RandomAccessIterator, class T, class Distance>
  1350. RandomAccessIterator __lower_bound (RandomAccessIterator first,
  1351.                                     RandomAccessIterator last, const T& value,
  1352.                                     Distance*, random_access_iterator_tag)
  1353. {
  1354.     Distance len = last - first;
  1355.     Distance half;
  1356.     RandomAccessIterator middle;
  1357.  
  1358.     while (len > 0)
  1359.     {
  1360.         half = len / 2;
  1361.         middle = first + half;
  1362.         if (*middle < value)
  1363.         {
  1364.             first = middle + 1;
  1365.             len = len - half - 1;
  1366.         }
  1367.         else
  1368.             len = half;
  1369.     }
  1370.     return first;
  1371. }
  1372.  
  1373. template <class ForwardIterator, class T, class Compare, class Distance>
  1374. ForwardIterator __lower_bound (ForwardIterator first, ForwardIterator last,
  1375.                                const T& value, Compare _RWSTD_COMP, Distance*,
  1376.                                forward_iterator_tag)
  1377. {
  1378.     Distance len;
  1379.     __initialize(len, Distance(0));
  1380.     distance(first, last, len);
  1381.     Distance half;
  1382.     ForwardIterator middle;
  1383.  
  1384.     while (len > 0)
  1385.     {
  1386.         half = len / 2;
  1387.         middle = first;
  1388.         advance(middle, half);
  1389.         if (_RWSTD_COMP(*middle, value))
  1390.         {
  1391.             first = middle;
  1392.             ++first;
  1393.             len = len - half - 1;
  1394.         }
  1395.         else
  1396.             len = half;
  1397.     }
  1398.     return first;
  1399. }
  1400.  
  1401. template <class RandomAccessIterator, class T, class Compare, class Distance>
  1402. RandomAccessIterator __lower_bound (RandomAccessIterator first,
  1403.                                     RandomAccessIterator last,
  1404.                                     const T& value, Compare _RWSTD_COMP, Distance*,
  1405.                                     random_access_iterator_tag)
  1406. {
  1407.     Distance len = last - first;
  1408.     Distance half;
  1409.     RandomAccessIterator middle;
  1410.  
  1411.     while (len > 0)
  1412.     {
  1413.         half = len / 2;
  1414.         middle = first + half;
  1415.         if (_RWSTD_COMP(*middle, value))
  1416.         {
  1417.             first = middle + 1;
  1418.             len = len - half - 1;
  1419.         }
  1420.         else
  1421.             len = half;
  1422.     }
  1423.     return first;
  1424. }
  1425.  
  1426. template <class ForwardIterator, class T, class Distance>
  1427. ForwardIterator __upper_bound (ForwardIterator first, ForwardIterator last,
  1428.                                const T& value, Distance*,
  1429.                                forward_iterator_tag)
  1430. {
  1431.     Distance len;
  1432.     __initialize(len, Distance(0));
  1433.     distance(first, last, len);
  1434.     Distance half;
  1435.     ForwardIterator middle;
  1436.  
  1437.     while (len > 0)
  1438.     {
  1439.         half = len / 2;
  1440.         middle = first;
  1441.         advance(middle, half);
  1442.         if (value < *middle)
  1443.             len = half;
  1444.         else
  1445.         {
  1446.             first = middle;
  1447.             ++first;
  1448.             len = len - half - 1;
  1449.         }
  1450.     }
  1451.     return first;
  1452. }
  1453.  
  1454. template <class RandomAccessIterator, class T, class Distance>
  1455. RandomAccessIterator __upper_bound (RandomAccessIterator first,
  1456.                                     RandomAccessIterator last, const T& value,
  1457.                                     Distance*, random_access_iterator_tag)
  1458. {
  1459.     Distance len = last - first;
  1460.     Distance half;
  1461.     RandomAccessIterator middle;
  1462.  
  1463.     while (len > 0)
  1464.     {
  1465.         half = len / 2;
  1466.         middle = first + half;
  1467.         if (value < *middle)
  1468.             len = half;
  1469.         else
  1470.         {
  1471.             first = middle + 1;
  1472.             len = len - half - 1;
  1473.         }
  1474.     }
  1475.     return first;
  1476. }
  1477.  
  1478. template <class ForwardIterator, class T, class Compare, class Distance>
  1479. ForwardIterator __upper_bound (ForwardIterator first, ForwardIterator last,
  1480.                                const T& value, Compare _RWSTD_COMP, Distance*,
  1481.                                forward_iterator_tag)
  1482. {
  1483.     Distance len;
  1484.     __initialize(len, Distance(0));
  1485.     distance(first, last, len);
  1486.     Distance half;
  1487.     ForwardIterator middle;
  1488.  
  1489.     while (len > 0)
  1490.     {
  1491.         half = len / 2;
  1492.         middle = first;
  1493.         advance(middle, half);
  1494.         if (_RWSTD_COMP(value, *middle))
  1495.             len = half;
  1496.         else {
  1497.             first = middle;
  1498.             ++first;
  1499.             len = len - half - 1;
  1500.         }
  1501.     }
  1502.     return first;
  1503. }
  1504.  
  1505. template <class RandomAccessIterator, class T, class Compare, class Distance>
  1506. RandomAccessIterator __upper_bound (RandomAccessIterator first,
  1507.                                     RandomAccessIterator last,
  1508.                                     const T& value, Compare _RWSTD_COMP, Distance*,
  1509.                                     random_access_iterator_tag)
  1510. {
  1511.     Distance len = last - first;
  1512.     Distance half;
  1513.     RandomAccessIterator middle;
  1514.  
  1515.     while (len > 0)
  1516.     {
  1517.         half = len / 2;
  1518.         middle = first + half;
  1519.         if (_RWSTD_COMP(value, *middle))
  1520.             len = half;
  1521.         else {
  1522.             first = middle + 1;
  1523.             len = len - half - 1;
  1524.         }
  1525.     }
  1526.     return first;
  1527. }
  1528.  
  1529. template <class ForwardIterator, class T, class Distance>
  1530. pair<ForwardIterator, ForwardIterator>
  1531. __equal_range (ForwardIterator first, ForwardIterator last, const T& value,
  1532.                Distance*, forward_iterator_tag)
  1533. {
  1534.     Distance len;
  1535.     __initialize(len, Distance(0));
  1536.     distance(first, last, len);
  1537.     Distance half;
  1538.     ForwardIterator middle, left, right;
  1539.  
  1540.     while (len > 0)
  1541.     {
  1542.         half = len / 2;
  1543.         middle = first;
  1544.         advance(middle, half);
  1545.         if (*middle < value)
  1546.         {
  1547.             first = middle;
  1548.             ++first;
  1549.             len = len - half - 1;
  1550.         }
  1551.         else if (value < *middle)
  1552.             len = half;
  1553.         else
  1554.         {
  1555.             left = lower_bound(first, middle, value);
  1556.             advance(first, len);
  1557.             right = upper_bound(++middle, first, value);
  1558.             pair<ForwardIterator, ForwardIterator> tmp(left, right);
  1559.             return tmp;
  1560.         }
  1561.     }
  1562.     pair<ForwardIterator, ForwardIterator> tmp(first, first);
  1563.     return tmp;
  1564. }
  1565.  
  1566. template <class RandomAccessIterator, class T, class Distance>
  1567. pair<RandomAccessIterator, RandomAccessIterator>
  1568. __equal_range (RandomAccessIterator first, RandomAccessIterator last,
  1569.                const T& value, Distance*, random_access_iterator_tag)
  1570. {
  1571.     Distance len = last - first;
  1572.     Distance half;
  1573.     RandomAccessIterator middle, left, right;
  1574.  
  1575.     while (len > 0)
  1576.     {
  1577.         half = len / 2;
  1578.         middle = first + half;
  1579.         if (*middle < value)
  1580.         {
  1581.             first = middle + 1;
  1582.             len = len - half - 1;
  1583.         }
  1584.         else if (value < *middle)
  1585.             len = half;
  1586.         else
  1587.         {
  1588.             left = lower_bound(first, middle, value);
  1589.             right = upper_bound(++middle, first + len, value);
  1590.             pair<RandomAccessIterator,RandomAccessIterator> tmp(left,right);
  1591.             return tmp;
  1592.         }
  1593.     }
  1594.     pair<RandomAccessIterator, RandomAccessIterator> tmp(first, first);
  1595.     return tmp;
  1596. }
  1597.  
  1598. template <class ForwardIterator, class T, class Compare, class Distance>
  1599. pair<ForwardIterator, ForwardIterator>
  1600. __equal_range (ForwardIterator first, ForwardIterator last, const T& value,
  1601.                Compare _RWSTD_COMP, Distance*, forward_iterator_tag)
  1602. {
  1603.     Distance len;
  1604.     __initialize(len, Distance(0));
  1605.     distance(first, last, len);
  1606.     Distance half;
  1607.     ForwardIterator middle, left, right;
  1608.  
  1609.     while (len > 0)
  1610.     {
  1611.         half = len / 2;
  1612.         middle = first;
  1613.         advance(middle, half);
  1614.         if (_RWSTD_COMP(*middle, value))
  1615.         {
  1616.             first = middle;
  1617.             ++first;
  1618.             len = len - half - 1;
  1619.         }
  1620.         else if (_RWSTD_COMP(value, *middle))
  1621.             len = half;
  1622.         else
  1623.         {
  1624.             left = lower_bound(first, middle, value, _RWSTD_COMP);
  1625.             advance(first, len);
  1626.             right = upper_bound(++middle, first, value, _RWSTD_COMP);
  1627.             pair<ForwardIterator, ForwardIterator> tmp(left, right);
  1628.             return tmp;
  1629.         }
  1630.     }
  1631.     pair<ForwardIterator, ForwardIterator> tmp(first, first);
  1632.     return tmp;
  1633. }           
  1634.  
  1635. template <class RandomAccessIterator, class T, class Compare, class Distance>
  1636. pair<RandomAccessIterator, RandomAccessIterator>
  1637. __equal_range (RandomAccessIterator first, RandomAccessIterator last,
  1638.                const T& value, Compare _RWSTD_COMP, Distance*,
  1639.                random_access_iterator_tag)
  1640. {
  1641.     Distance len = last - first;
  1642.     Distance half;
  1643.     RandomAccessIterator middle, left, right;
  1644.  
  1645.     while (len > 0)
  1646.     {
  1647.         half = len / 2;
  1648.         middle = first + half;
  1649.         if (_RWSTD_COMP(*middle, value))
  1650.         {
  1651.             first = middle + 1;
  1652.             len = len - half - 1;
  1653.         }
  1654.         else if (_RWSTD_COMP(value, *middle))
  1655.             len = half;
  1656.         else
  1657.         {
  1658.             left = lower_bound(first, middle, value, _RWSTD_COMP);
  1659.             right = upper_bound(++middle, first + len, value, _RWSTD_COMP);
  1660.             pair<RandomAccessIterator,RandomAccessIterator> tmp(left, right);
  1661.             return tmp;
  1662.         }
  1663.     }
  1664.     pair<RandomAccessIterator, RandomAccessIterator> tmp(first, first);
  1665.     return tmp;
  1666. }           
  1667.  
  1668. //
  1669. // Merge
  1670. //
  1671.  
  1672. template <class InputIterator1, class InputIterator2, class OutputIterator>
  1673. OutputIterator merge (InputIterator1 first1, InputIterator1 last1,
  1674.                       InputIterator2 first2, InputIterator2 last2,
  1675.                       OutputIterator result)
  1676. {
  1677.     while (first1 != last1 && first2 != last2)
  1678.     {
  1679.         if (*first2 < *first1)
  1680.             *result++ = *first2++;
  1681.         else
  1682.             *result++ = *first1++;
  1683.     }
  1684.     return copy(first2, last2, copy(first1, last1, result));
  1685. }
  1686.  
  1687. template <class InputIterator1, class InputIterator2, class OutputIterator,
  1688.           class Compare>
  1689. OutputIterator merge (InputIterator1 first1, InputIterator1 last1,
  1690.                       InputIterator2 first2, InputIterator2 last2,
  1691.                       OutputIterator result, Compare _RWSTD_COMP)
  1692. {
  1693.     while (first1 != last1 && first2 != last2)
  1694.     {
  1695.         if (_RWSTD_COMP(*first2, *first1))
  1696.             *result++ = *first2++;
  1697.         else
  1698.             *result++ = *first1++;
  1699.     }
  1700.     return copy(first2, last2, copy(first1, last1, result));
  1701. }
  1702.  
  1703. template <class BidirectionalIterator, class Distance>
  1704. void __merge_without_buffer (BidirectionalIterator first,
  1705.                              BidirectionalIterator middle,
  1706.                              BidirectionalIterator last,
  1707.                              Distance len1, Distance len2)
  1708. {
  1709.     if (len1 == 0 || len2 == 0) return;
  1710.     if (len1 + len2 == 2)
  1711.     {
  1712.         if (*middle < *first) iter_swap(first, middle);
  1713.         return;
  1714.     }
  1715.     BidirectionalIterator first_cut = first;
  1716.     BidirectionalIterator second_cut = middle;
  1717.     Distance len11;
  1718.     __initialize(len11, Distance(0));
  1719.     Distance len22;
  1720.     __initialize(len22, Distance(0));
  1721.     if (len1 > len2)
  1722.     {
  1723.         len11 = len1 / 2;
  1724.         advance(first_cut, len11);
  1725.         second_cut = lower_bound(middle, last, *first_cut);
  1726.         distance(middle, second_cut, len22);
  1727.     }
  1728.     else
  1729.     {
  1730.         len22 = len2 / 2;
  1731.         advance(second_cut, len22);
  1732.         first_cut = upper_bound(first, middle, *second_cut);
  1733.         distance(first, first_cut, len11);
  1734.     }
  1735.     rotate(first_cut, middle, second_cut);
  1736.     BidirectionalIterator new_middle = first_cut;
  1737.     advance(new_middle, len22);
  1738.     __merge_without_buffer(first, first_cut, new_middle, len11, len22);
  1739.     __merge_without_buffer(new_middle, second_cut, last, len1 - len11,
  1740.                            len2 - len22);
  1741. }
  1742.  
  1743. template <class BidirectionalIterator, class Distance, class Compare>
  1744. void __merge_without_buffer (BidirectionalIterator first,
  1745.                              BidirectionalIterator middle,
  1746.                              BidirectionalIterator last,
  1747.                              Distance len1, Distance len2, Compare _RWSTD_COMP)
  1748. {
  1749.     if (len1 == 0 || len2 == 0) return;
  1750.     if (len1 + len2 == 2)
  1751.     {
  1752.         if (_RWSTD_COMP(*middle, *first)) iter_swap(first, middle);
  1753.         return;
  1754.     }
  1755.     BidirectionalIterator first_cut = first;
  1756.     BidirectionalIterator second_cut = middle;
  1757.     Distance len11;
  1758.     __initialize(len11, Distance(0));
  1759.     Distance len22;
  1760.     __initialize(len22, Distance(0));
  1761.     if (len1 > len2)
  1762.     {
  1763.         len11 = len1 / 2;
  1764.         advance(first_cut, len11);
  1765.         second_cut = lower_bound(middle, last, *first_cut, _RWSTD_COMP);
  1766.         distance(middle, second_cut, len22);
  1767.     }
  1768.     else
  1769.     {
  1770.         len22 = len2 / 2;
  1771.         advance(second_cut, len22);
  1772.         first_cut = upper_bound(first, middle, *second_cut, _RWSTD_COMP);
  1773.         distance(first, first_cut, len11);
  1774.     }
  1775.     rotate(first_cut, middle, second_cut);
  1776.     BidirectionalIterator new_middle = first_cut;
  1777.     advance(new_middle, len22);
  1778.     __merge_without_buffer(first, first_cut, new_middle, len11, len22, _RWSTD_COMP);
  1779.     __merge_without_buffer(new_middle, second_cut, last, len1 - len11,
  1780.                            len2 - len22, _RWSTD_COMP);
  1781. }
  1782.  
  1783. template <class BidirectionalIterator1, class BidirectionalIterator2,
  1784.           class Distance>
  1785. BidirectionalIterator1 __rotate_adaptive (BidirectionalIterator1 first,
  1786.                                           BidirectionalIterator1 middle,
  1787.                                           BidirectionalIterator1 last,
  1788.                                           Distance len1, Distance len2,
  1789.                                           BidirectionalIterator2 buffer,
  1790.                                           Distance buffer_size)
  1791. {
  1792.     BidirectionalIterator2 buffer_end;
  1793.     if (len1 > len2 && len2 <= buffer_size)
  1794.     {
  1795.         buffer_end = copy(middle, last, buffer);
  1796.         copy_backward(first, middle, last);
  1797.         return copy(buffer, buffer_end, first);
  1798.     }
  1799.     else if (len1 <= buffer_size)
  1800.     {
  1801.         buffer_end = copy(first, middle, buffer);
  1802.         copy(middle, last, first);
  1803.         return copy_backward(buffer, buffer_end, last);
  1804.     }
  1805.     else
  1806.     {
  1807.         rotate(first, middle, last);
  1808.         advance(first, len2);
  1809.         return first;
  1810.     }
  1811. }
  1812.  
  1813. template <class BidirectionalIterator1, class BidirectionalIterator2,
  1814.           class BidirectionalIterator3>
  1815. BidirectionalIterator3 __merge_backward (BidirectionalIterator1 first1,
  1816.                                          BidirectionalIterator1 last1,
  1817.                                          BidirectionalIterator2 first2,
  1818.                                          BidirectionalIterator2 last2,
  1819.                                          BidirectionalIterator3 result)
  1820. {
  1821.     if (first1 == last1) return copy_backward(first2, last2, result);
  1822.     if (first2 == last2) return copy_backward(first1, last1, result);
  1823.     --last1;
  1824.     --last2;
  1825.     while (true)
  1826.     {
  1827.         if (*last2 < *last1)
  1828.         {
  1829.             *--result = *last1;
  1830.             if (first1 == last1) return copy_backward(first2, ++last2, result);
  1831.             --last1;
  1832.         }
  1833.         else
  1834.         {
  1835.             *--result = *last2;
  1836.             if (first2 == last2) return copy_backward(first1, ++last1, result);
  1837.             --last2;
  1838.         }
  1839.     }
  1840. }
  1841.  
  1842. template <class BidirectionalIterator1, class BidirectionalIterator2,
  1843.           class BidirectionalIterator3, class Compare>
  1844. BidirectionalIterator3 __merge_backward (BidirectionalIterator1 first1,
  1845.                                          BidirectionalIterator1 last1,
  1846.                                          BidirectionalIterator2 first2,
  1847.                                          BidirectionalIterator2 last2,
  1848.                                          BidirectionalIterator3 result,
  1849.                                          Compare _RWSTD_COMP)
  1850. {
  1851.     if (first1 == last1) return copy_backward(first2, last2, result);
  1852.     if (first2 == last2) return copy_backward(first1, last1, result);
  1853.     --last1;
  1854.     --last2;
  1855.     while (true)
  1856.     {
  1857.         if (_RWSTD_COMP(*last2, *last1))
  1858.         {
  1859.             *--result = *last1;
  1860.             if (first1 == last1) return copy_backward(first2, ++last2, result);
  1861.             --last1;
  1862.         }
  1863.         else
  1864.         {
  1865.             *--result = *last2;
  1866.             if (first2 == last2) return copy_backward(first1, ++last1, result);
  1867.             --last2;
  1868.         }
  1869.     }
  1870. }
  1871.  
  1872. template <class BidirectionalIterator, class Distance, class Pointer, class T>
  1873. void __merge_adaptive (BidirectionalIterator first,
  1874.                        BidirectionalIterator middle,
  1875.                        BidirectionalIterator last, Distance len1,Distance len2,
  1876.                        Pointer buffer, Distance buffer_size, T*)
  1877. {
  1878.     if (len1 <= len2 && len1 <= buffer_size)
  1879.     {
  1880.         Pointer end_buffer = copy(first, middle, buffer);
  1881.         merge(buffer, end_buffer, middle, last, first);
  1882.     }
  1883.     else if (len2 <= buffer_size)
  1884.     {
  1885.         Pointer end_buffer = copy(middle, last, buffer);
  1886.         __merge_backward(first, middle, buffer, end_buffer, last);
  1887.     }
  1888.     else
  1889.     {
  1890.         BidirectionalIterator first_cut = first;
  1891.         BidirectionalIterator second_cut = middle;
  1892.         Distance len11;
  1893.         __initialize(len11, Distance(0));
  1894.         Distance len22;
  1895.         __initialize(len22, Distance(0));
  1896.         if (len1 > len2)
  1897.         {
  1898.             len11 = len1 / 2;
  1899.             advance(first_cut, len11);
  1900.             second_cut = lower_bound(middle, last, *first_cut);
  1901.             distance(middle, second_cut, len22);   
  1902.         }
  1903.         else
  1904.         {
  1905.             len22 = len2 / 2;
  1906.             advance(second_cut, len22);
  1907.             first_cut = upper_bound(first, middle, *second_cut);
  1908.             distance(first, first_cut, len11);
  1909.         }
  1910.         BidirectionalIterator new_middle =
  1911.       __rotate_adaptive(first_cut, middle, second_cut, len1 - len11,
  1912.                 len22, buffer, buffer_size);
  1913.     __merge_adaptive(first, first_cut, new_middle, len11, len22, buffer,
  1914.                          buffer_size, _RWSTD_STATIC_CAST(T*,0));
  1915.         __merge_adaptive(new_middle, second_cut, last, len1 - len11,
  1916.                          len2 - len22, buffer, buffer_size, _RWSTD_STATIC_CAST(T*,0));
  1917.     }
  1918. }
  1919.  
  1920. template <class BidirectionalIterator, class Distance, class Pointer, class T,
  1921.           class Compare>
  1922. void __merge_adaptive (BidirectionalIterator first,
  1923.                        BidirectionalIterator middle,
  1924.                        BidirectionalIterator last, Distance len1,Distance len2,
  1925.                        Pointer buffer, Distance buffer_size, T*, Compare _RWSTD_COMP)
  1926. {
  1927.     if (len1 <= len2 && len1 <= buffer_size)
  1928.     {
  1929.         Pointer end_buffer = copy(first, middle, buffer);
  1930.         merge(buffer, end_buffer, middle, last, first, _RWSTD_COMP);
  1931.     }
  1932.     else if (len2 <= buffer_size)
  1933.     {
  1934.         Pointer end_buffer = copy(middle, last, buffer);
  1935.         __merge_backward(first, middle, buffer, end_buffer, last, _RWSTD_COMP);
  1936.     }
  1937.     else
  1938.     {
  1939.         BidirectionalIterator first_cut = first;
  1940.         BidirectionalIterator second_cut = middle;
  1941.         Distance len11;
  1942.         __initialize(len11, Distance(0));
  1943.         Distance len22;
  1944.         __initialize(len22, Distance(0));
  1945.         if (len1 > len2)
  1946.         {
  1947.             len11 = len1 / 2;
  1948.             advance(first_cut, len11);
  1949.             second_cut = lower_bound(middle, last, *first_cut, _RWSTD_COMP);
  1950.             distance(middle, second_cut, len22);   
  1951.         }
  1952.         else
  1953.         {
  1954.             len22 = len2 / 2;
  1955.             advance(second_cut, len22);
  1956.             first_cut = upper_bound(first, middle, *second_cut, _RWSTD_COMP);
  1957.             distance(first, first_cut, len11);
  1958.         }
  1959.         BidirectionalIterator new_middle =
  1960.             __rotate_adaptive(first_cut, middle, second_cut, len1 - len11,
  1961.                               len22, buffer, buffer_size);
  1962.         __merge_adaptive(first, first_cut, new_middle, len11, len22, buffer,
  1963.                          buffer_size, _RWSTD_STATIC_CAST(T*,0), _RWSTD_COMP);
  1964.         __merge_adaptive(new_middle, second_cut, last, len1 - len11,
  1965.                          len2 - len22, buffer, buffer_size, _RWSTD_STATIC_CAST(T*,0), _RWSTD_COMP);
  1966.     }
  1967. }
  1968.  
  1969. template <class BidirectionalIterator, class Distance, class Pointer, class T>
  1970. void __inplace_merge (BidirectionalIterator first,
  1971.                       BidirectionalIterator middle, 
  1972.                       BidirectionalIterator last, Distance len1,
  1973.                       Distance len2, pair<Pointer, Distance> p, T*)
  1974. {
  1975.     if (p.first == 0)
  1976.         __merge_without_buffer(first, middle, last, len1, len2);
  1977.     else
  1978.     {
  1979.         Distance len = min(p.second, len1 + len2);
  1980.         fill_n(raw_storage_iterator<Pointer, T>(p.first), len, *first);
  1981.         __merge_adaptive(first, middle, last, len1, len2, p.first,
  1982.                          p.second, _RWSTD_STATIC_CAST(T*,0));
  1983.         destroy(p.first, p.first + len);
  1984.         return_temporary_buffer(p.first);
  1985.     }
  1986. }
  1987.  
  1988. template <class BidirectionalIterator, class Distance, class Pointer, class T,
  1989.       class Compare>
  1990. void __inplace_merge (BidirectionalIterator first,
  1991.                       BidirectionalIterator middle,
  1992.                       BidirectionalIterator last, Distance len1,
  1993.                       Distance len2, pair<Pointer, Distance> p, T*,
  1994.                       Compare _RWSTD_COMP)
  1995. {
  1996.     if (p.first == 0)
  1997.         __merge_without_buffer(first, middle, last, len1, len2, _RWSTD_COMP);
  1998.     else
  1999.     {
  2000.         Distance len = min(p.second, len1 + len2);
  2001.         fill_n(raw_storage_iterator<Pointer, T>(p.first), len, *first);
  2002.         __merge_adaptive(first, middle, last, len1, len2, p.first,
  2003.                          p.second, _RWSTD_STATIC_CAST(T*,0), _RWSTD_COMP); 
  2004.         destroy(p.first, p.first + len);
  2005.         return_temporary_buffer(p.first);
  2006.     }
  2007. }
  2008.  
  2009. //
  2010. // Set operations.
  2011. //
  2012.  
  2013. template <class InputIterator1, class InputIterator2>
  2014. bool includes (InputIterator1 first1, InputIterator1 last1,
  2015.                InputIterator2 first2, InputIterator2 last2)
  2016. {
  2017.     while (first1 != last1 && first2 != last2)
  2018.     {
  2019.         if (*first2 < *first1)
  2020.             return false;
  2021.         else if(*first1 < *first2) 
  2022.             ++first1;
  2023.         else
  2024.             ++first1, ++first2;
  2025.     }
  2026.     return first2 == last2;
  2027. }
  2028.  
  2029. template <class InputIterator1, class InputIterator2, class Compare>
  2030. bool includes (InputIterator1 first1, InputIterator1 last1,
  2031.                InputIterator2 first2, InputIterator2 last2, Compare _RWSTD_COMP)
  2032. {
  2033.     while (first1 != last1 && first2 != last2)
  2034.     {
  2035.         if (_RWSTD_COMP(*first2, *first1))
  2036.             return false;
  2037.         else if(_RWSTD_COMP(*first1, *first2)) 
  2038.             ++first1;
  2039.         else
  2040.             ++first1, ++first2;
  2041.     }
  2042.     return first2 == last2;
  2043. }
  2044.  
  2045. template <class InputIterator1, class InputIterator2, class OutputIterator>
  2046. OutputIterator set_union (InputIterator1 first1, InputIterator1 last1,
  2047.                           InputIterator2 first2, InputIterator2 last2,
  2048.                           OutputIterator result)
  2049. {
  2050.     while (first1 != last1 && first2 != last2)
  2051.     {
  2052.         if (*first1 < *first2)
  2053.             *result++ = *first1++;
  2054.         else if (*first2 < *first1)
  2055.             *result++ = *first2++;
  2056.         else
  2057.         {
  2058.             *result++ = *first1++;
  2059.             first2++;
  2060.         }
  2061.     }
  2062.     return copy(first2, last2, copy(first1, last1, result));
  2063. }
  2064.  
  2065. template <class InputIterator1, class InputIterator2, class OutputIterator,
  2066.           class Compare>
  2067. OutputIterator set_union (InputIterator1 first1, InputIterator1 last1,
  2068.                           InputIterator2 first2, InputIterator2 last2,
  2069.                           OutputIterator result, Compare _RWSTD_COMP)
  2070. {
  2071.     while (first1 != last1 && first2 != last2)
  2072.     {
  2073.         if (_RWSTD_COMP(*first1, *first2))
  2074.             *result++ = *first1++;
  2075.         else if (_RWSTD_COMP(*first2, *first1))
  2076.             *result++ = *first2++;
  2077.         else
  2078.         {
  2079.             *result++ = *first1++;
  2080.             ++first2;
  2081.         }
  2082.     }
  2083.     return copy(first2, last2, copy(first1, last1, result));
  2084. }
  2085.  
  2086. template <class InputIterator1, class InputIterator2, class OutputIterator>
  2087. OutputIterator set_intersection (InputIterator1 first1, InputIterator1 last1,
  2088.                                  InputIterator2 first2, InputIterator2 last2,
  2089.                                  OutputIterator result)
  2090. {
  2091.     while (first1 != last1 && first2 != last2)
  2092.     {
  2093.         if (*first1 < *first2)
  2094.             ++first1;
  2095.         else if (*first2 < *first1)
  2096.             ++first2;
  2097.         else
  2098.         {
  2099.             *result++ = *first1++;
  2100.             ++first2;
  2101.         }
  2102.     }
  2103.     return result;
  2104. }
  2105.  
  2106. template <class InputIterator1, class InputIterator2, class OutputIterator,
  2107.           class Compare>
  2108. OutputIterator set_intersection (InputIterator1 first1, InputIterator1 last1,
  2109.                                  InputIterator2 first2, InputIterator2 last2,
  2110.                                  OutputIterator result, Compare _RWSTD_COMP)
  2111. {
  2112.     while (first1 != last1 && first2 != last2)
  2113.     {
  2114.         if (_RWSTD_COMP(*first1, *first2))
  2115.             ++first1;
  2116.         else if (_RWSTD_COMP(*first2, *first1))
  2117.             ++first2;
  2118.         else
  2119.         {
  2120.             *result++ = *first1++;
  2121.             ++first2;
  2122.         }
  2123.     }
  2124.     return result;
  2125. }
  2126.  
  2127. template <class InputIterator1, class InputIterator2, class OutputIterator>
  2128. OutputIterator set_difference (InputIterator1 first1, InputIterator1 last1,
  2129.                                InputIterator2 first2, InputIterator2 last2,
  2130.                                OutputIterator result)
  2131. {
  2132.     while (first1 != last1 && first2 != last2)
  2133.     {
  2134.         if (*first1 < *first2)
  2135.             *result++ = *first1++;
  2136.         else if (*first2 < *first1)
  2137.             ++first2;
  2138.         else
  2139.         {
  2140.             ++first1;
  2141.             ++first2;
  2142.         }
  2143.     }
  2144.     return copy(first1, last1, result);
  2145. }
  2146.  
  2147. template <class InputIterator1, class InputIterator2, class OutputIterator, 
  2148.           class Compare>
  2149. OutputIterator set_difference (InputIterator1 first1, InputIterator1 last1,
  2150.                                InputIterator2 first2, InputIterator2 last2, 
  2151.                                OutputIterator result, Compare _RWSTD_COMP)
  2152. {
  2153.     while (first1 != last1 && first2 != last2)
  2154.     {
  2155.         if (_RWSTD_COMP(*first1, *first2))
  2156.             *result++ = *first1++;
  2157.         else if (_RWSTD_COMP(*first2, *first1))
  2158.             ++first2;
  2159.         else
  2160.         {
  2161.             ++first1;
  2162.             ++first2;
  2163.         }
  2164.     }
  2165.     return copy(first1, last1, result);
  2166. }
  2167.  
  2168. template <class InputIterator1, class InputIterator2, class OutputIterator>
  2169. OutputIterator set_symmetric_difference (InputIterator1 first1,
  2170.                                          InputIterator1 last1,
  2171.                                          InputIterator2 first2,
  2172.                                          InputIterator2 last2,
  2173.                                          OutputIterator result)
  2174. {
  2175.     while (first1 != last1 && first2 != last2)
  2176.     {
  2177.         if (*first1 < *first2)
  2178.             *result++ = *first1++;
  2179.         else if (*first2 < *first1)
  2180.             *result++ = *first2++;
  2181.         else
  2182.         {
  2183.             ++first1;
  2184.             ++first2;
  2185.         }
  2186.     }
  2187.     return copy(first2, last2, copy(first1, last1, result));
  2188. }
  2189.  
  2190. template <class InputIterator1, class InputIterator2, class OutputIterator,
  2191.           class Compare>
  2192. OutputIterator set_symmetric_difference (InputIterator1 first1,
  2193.                                          InputIterator1 last1,
  2194.                                          InputIterator2 first2,
  2195.                                          InputIterator2 last2,
  2196.                                          OutputIterator result, Compare _RWSTD_COMP)
  2197. {
  2198.     while (first1 != last1 && first2 != last2)
  2199.     {
  2200.         if (_RWSTD_COMP(*first1, *first2))
  2201.             *result++ = *first1++;
  2202.         else if (_RWSTD_COMP(*first2, *first1))
  2203.             *result++ = *first2++;
  2204.         else
  2205.         {
  2206.             ++first1;
  2207.             ++first2;
  2208.         }
  2209.     }
  2210.     return copy(first2, last2, copy(first1, last1, result));
  2211. }
  2212.  
  2213. //
  2214. // Heap operations.
  2215. //
  2216.  
  2217. template <class RandomAccessIterator, class Distance, class T>
  2218. void __push_heap (RandomAccessIterator first, Distance holeIndex,
  2219.                   Distance topIndex, T value)
  2220. {
  2221.     Distance parent = (holeIndex - 1) / 2;
  2222.     while (holeIndex > topIndex && *(first + parent) < value)
  2223.     {
  2224.         *(first + holeIndex) = *(first + parent);
  2225.         holeIndex = parent;
  2226.         parent = (holeIndex - 1) / 2;
  2227.     }    
  2228.     *(first + holeIndex) = value;
  2229. }
  2230.  
  2231. template <class RandomAccessIterator, class Distance, class T, class Compare>
  2232. void __push_heap (RandomAccessIterator first, Distance holeIndex,
  2233.                   Distance topIndex, T value, Compare _RWSTD_COMP)
  2234. {
  2235.     Distance parent = (holeIndex - 1) / 2;
  2236.     while (holeIndex > topIndex && _RWSTD_COMP(*(first + parent), value))
  2237.     {
  2238.         *(first + holeIndex) = *(first + parent);
  2239.         holeIndex = parent;
  2240.         parent = (holeIndex - 1) / 2;
  2241.     }
  2242.     *(first + holeIndex) = value;
  2243. }
  2244.  
  2245. template <class RandomAccessIterator, class Distance, class T>
  2246. void __adjust_heap (RandomAccessIterator first, Distance holeIndex,
  2247.                     Distance len, T value)
  2248. {
  2249.     Distance topIndex = holeIndex;
  2250.     Distance secondChild = 2 * holeIndex + 2;
  2251.     while (secondChild < len)
  2252.     {
  2253.         if (*(first + secondChild) < *(first + (secondChild - 1)))
  2254.             secondChild--;
  2255.         *(first + holeIndex) = *(first + secondChild);
  2256.         holeIndex = secondChild;
  2257.         secondChild = 2 * (secondChild + 1);
  2258.     }
  2259.     if (secondChild == len)
  2260.     {
  2261.         *(first + holeIndex) = *(first + (secondChild - 1));
  2262.         holeIndex = secondChild - 1;
  2263.     }
  2264.     __push_heap(first, holeIndex, topIndex, value);
  2265. }
  2266.  
  2267. template <class RandomAccessIterator, class Distance, class T, class Compare>
  2268. void __adjust_heap (RandomAccessIterator first, Distance holeIndex,
  2269.                     Distance len, T value, Compare _RWSTD_COMP)
  2270. {
  2271.     Distance topIndex = holeIndex;
  2272.     Distance secondChild = 2 * holeIndex + 2;
  2273.     while (secondChild < len)
  2274.     {
  2275.         if (_RWSTD_COMP(*(first + secondChild), *(first + (secondChild - 1))))
  2276.             secondChild--;
  2277.         *(first + holeIndex) = *(first + secondChild);
  2278.         holeIndex = secondChild;
  2279.         secondChild = 2 * (secondChild + 1);
  2280.     }
  2281.     if (secondChild == len)
  2282.     {
  2283.         *(first + holeIndex) = *(first + (secondChild - 1));
  2284.         holeIndex = secondChild - 1;
  2285.     }
  2286.     __push_heap(first, holeIndex, topIndex, value, _RWSTD_COMP);
  2287. }
  2288.  
  2289. template <class RandomAccessIterator, class T, class Distance>
  2290. void __make_heap (RandomAccessIterator first, RandomAccessIterator last, T*,
  2291.                   Distance*)
  2292. {
  2293.     Distance len = last - first;
  2294.     Distance parent = (len - 2)/2;
  2295.     while (true)
  2296.     {
  2297.         __adjust_heap(first, parent, len, T(*(first + parent)));
  2298.         if (parent == 0) return;
  2299.         parent--;
  2300.     }
  2301. }
  2302.  
  2303. template <class RandomAccessIterator, class Compare, class T, class Distance>
  2304. void __make_heap (RandomAccessIterator first, RandomAccessIterator last,
  2305.                   Compare _RWSTD_COMP, T*, Distance*)
  2306. {
  2307.     Distance len = last - first;
  2308.     Distance parent = (len - 2)/2;
  2309.     while (true)
  2310.     {
  2311.         __adjust_heap(first, parent, len, T(*(first + parent)), _RWSTD_COMP);
  2312.         if (parent == 0)
  2313.             return;
  2314.         parent--;
  2315.     }
  2316. }
  2317.  
  2318. template <class RandomAccessIterator>
  2319. void sort_heap (RandomAccessIterator first, RandomAccessIterator last)
  2320. {
  2321.     while (last - first > 1) pop_heap(first, last--);
  2322. }
  2323.  
  2324. template <class RandomAccessIterator, class Compare>
  2325. void sort_heap (RandomAccessIterator first, RandomAccessIterator last,
  2326.                 Compare _RWSTD_COMP)
  2327. {
  2328.     while (last - first > 1) pop_heap(first, last--, _RWSTD_COMP);
  2329. }
  2330.  
  2331. //
  2332. // Minimum and maximum.
  2333. //
  2334.  
  2335. template <class ForwardIterator>
  2336. ForwardIterator min_element (ForwardIterator first, ForwardIterator last)
  2337. {
  2338.     if (first == last) return first;
  2339.     ForwardIterator result = first;
  2340.     while (++first != last) 
  2341.         if (*first < *result) result = first;
  2342.     return result;
  2343. }
  2344.  
  2345. template <class ForwardIterator, class Compare>
  2346. ForwardIterator min_element (ForwardIterator first, ForwardIterator last,
  2347.                              Compare _RWSTD_COMP)
  2348. {
  2349.     if (first == last) return first;
  2350.     ForwardIterator result = first;
  2351.     while (++first != last) 
  2352.         if (_RWSTD_COMP(*first, *result)) result = first;
  2353.     return result;
  2354. }
  2355.  
  2356. template <class ForwardIterator>
  2357. ForwardIterator max_element (ForwardIterator first, ForwardIterator last)
  2358. {
  2359.     if (first == last) return first;
  2360.     ForwardIterator result = first;
  2361.     while (++first != last) 
  2362.         if (*result < *first) result = first;
  2363.     return result;
  2364. }
  2365.  
  2366. template <class ForwardIterator, class Compare>
  2367. ForwardIterator max_element (ForwardIterator first, ForwardIterator last,
  2368.                              Compare _RWSTD_COMP)
  2369. {
  2370.     if (first == last) return first;
  2371.     ForwardIterator result = first;
  2372.     while (++first != last) 
  2373.         if (_RWSTD_COMP(*result, *first)) result = first;
  2374.     return result;
  2375. }
  2376.  
  2377. template <class InputIterator1, class InputIterator2>
  2378. bool lexicographical_compare (InputIterator1 first1, InputIterator1 last1,
  2379.                               InputIterator2 first2, InputIterator2 last2)
  2380. {
  2381.     while (first1 != last1 && first2 != last2)
  2382.     {
  2383.         if (*first1 < *first2)     return true;
  2384.         if (*first2++ < *first1++) return false;
  2385.     }
  2386.     return first1 == last1 && first2 != last2;
  2387. }
  2388.  
  2389. template <class InputIterator1, class InputIterator2, class Compare>
  2390. bool lexicographical_compare(InputIterator1 first1, InputIterator1 last1,
  2391.                              InputIterator2 first2, InputIterator2 last2,
  2392.                              Compare _RWSTD_COMP)
  2393. {
  2394.     while (first1 != last1 && first2 != last2)
  2395.     {
  2396.         if (_RWSTD_COMP(*first1, *first2))     return true;
  2397.         if (_RWSTD_COMP(*first2++, *first1++)) return false;
  2398.     }
  2399.     return first1 == last1 && first2 != last2;
  2400. }
  2401.  
  2402. //
  2403. // Permutations.
  2404. //
  2405.  
  2406. template <class BidirectionalIterator>
  2407. bool next_permutation (BidirectionalIterator first,
  2408.                        BidirectionalIterator last)
  2409. {
  2410.     if (first == last) return false;
  2411.     BidirectionalIterator i = first;
  2412.     ++i;
  2413.     if (i == last) return false;
  2414.     i = last;
  2415.     --i;
  2416.  
  2417.     for (;;)
  2418.     {
  2419.         BidirectionalIterator ii = i--;
  2420.         if (*i < *ii)
  2421.         {
  2422.             BidirectionalIterator j = last;
  2423.             while (!(*i < *--j))
  2424.                 ;
  2425.             iter_swap(i, j);
  2426.             reverse(ii, last);
  2427.             return true;
  2428.         }
  2429.         if (i == first)
  2430.         {
  2431.             reverse(first, last);
  2432.             return false;
  2433.         }
  2434.     }
  2435. }
  2436.  
  2437. template <class BidirectionalIterator, class Compare>
  2438. bool next_permutation (BidirectionalIterator first, BidirectionalIterator last,
  2439.                        Compare _RWSTD_COMP)
  2440. {
  2441.     if (first == last) return false;
  2442.     BidirectionalIterator i = first;
  2443.     ++i;
  2444.     if (i == last) return false;
  2445.     i = last;
  2446.     --i;
  2447.  
  2448.     for (;;)
  2449.     {
  2450.         BidirectionalIterator ii = i--;
  2451.         if (_RWSTD_COMP(*i, *ii))
  2452.         {
  2453.             BidirectionalIterator j = last;
  2454.             while (!_RWSTD_COMP(*i, *--j))
  2455.                 ;
  2456.             iter_swap(i, j);
  2457.             reverse(ii, last);
  2458.             return true;
  2459.         }
  2460.         if (i == first)
  2461.         {
  2462.             reverse(first, last);
  2463.             return false;
  2464.         }
  2465.     }
  2466. }
  2467.  
  2468. template <class BidirectionalIterator>
  2469. bool prev_permutation (BidirectionalIterator first,
  2470.                        BidirectionalIterator last)
  2471. {
  2472.     if (first == last) return false;
  2473.     BidirectionalIterator i = first;
  2474.     ++i;
  2475.     if (i == last) return false;
  2476.     i = last;
  2477.     --i;
  2478.  
  2479.     for (;;)
  2480.     {
  2481.         BidirectionalIterator ii = i--;
  2482.         if (*ii < *i)
  2483.         {
  2484.             BidirectionalIterator j = last;
  2485.             while (!(*--j < *i))
  2486.                 ;
  2487.             iter_swap(i, j);
  2488.             reverse(ii, last);
  2489.             return true;
  2490.         }
  2491.         if (i == first)
  2492.         {
  2493.             reverse(first, last);
  2494.             return false;
  2495.         }
  2496.     }
  2497. }
  2498.  
  2499. template <class BidirectionalIterator, class Compare>
  2500. bool prev_permutation (BidirectionalIterator first, BidirectionalIterator last,
  2501.                        Compare _RWSTD_COMP)
  2502. {
  2503.     if (first == last) return false;
  2504.     BidirectionalIterator i = first;
  2505.     ++i;
  2506.     if (i == last) return false;
  2507.     i = last;
  2508.     --i;
  2509.  
  2510.     for(;;)
  2511.     {
  2512.         BidirectionalIterator ii = i--;
  2513.         if (_RWSTD_COMP(*ii, *i))
  2514.         {
  2515.             BidirectionalIterator j = last;
  2516.             while (!_RWSTD_COMP(*--j, *i))
  2517.                 ;
  2518.             iter_swap(i, j);
  2519.             reverse(ii, last);
  2520.             return true;
  2521.         }
  2522.         if (i == first)
  2523.         {
  2524.             reverse(first, last);
  2525.             return false;
  2526.         }
  2527.     }
  2528. }
  2529.  
  2530. #ifndef _RWSTD_NO_NAMESPACE
  2531. }
  2532. #endif
  2533.  
  2534. #pragma option pop
  2535. #endif /* __ALGORITH_CC */